<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
    xmlns:a="attribute"
    xmlns:o="object"
    xmlns:c="collection"
>

  <xsl:output method="text" indent="yes" encoding="utf-8" version="1.0"/>


  <xsl:template match="/" >
    <xsl:for-each select="//c:Entities">
      <xsl:call-template name="Entitie"></xsl:call-template>
    </xsl:for-each>
  </xsl:template>
  <xsl:template name="Entitie">
    <xsl:for-each select="o:Entity">
      create table [<xsl:value-of select="a:Name"/>](
      <xsl:for-each select="c:Attributes/o:EntityAttribute">
        <xsl:if test="position()!=1">,</xsl:if>
        [<xsl:value-of select="a:Name"/>] <xsl:choose>
          <xsl:when test="a:DataType='Identity'">varchar(32) unique primary key</xsl:when>
		  <xsl:when test="a:DataType='Unique'">varchar(32) unique</xsl:when>
          <xsl:when test="a:DataType='I'">integer</xsl:when>
          <xsl:when test="a:DataType='F'">float</xsl:when>
          <xsl:when test="a:DataType='BL'">bool</xsl:when>
          <xsl:when test="a:DataType='D'">date</xsl:when>
          <xsl:when test="a:DataType='DT'">timestamp</xsl:when>
          <xsl:when test="contains(a:DataType,'TXT')">nvarchar(<xsl:value-of select="a:Length"/>)</xsl:when>
          <xsl:when test="contains(a:DataType,'Text')">nvarchar(2000)</xsl:when>
        </xsl:choose>
      </xsl:for-each>
      );
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>